//
//  YHKTabBarViewController.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/2/28.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit

class YHKTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
    
        //自定义tabBar
      let tabbar = YHKTabBar()
        
        
//        tabBar = YHKTabBar()  只读属性，不能给系统的tabBar赋值
        //利用KVC可以成功为只读属性赋值
        setValue(tabbar, forKey: "tabBar")
        
        //实现按钮的点击事件
        tabbar.addButton.addTarget(self, action: "showPublish", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置四大控制器
      addChildViewController()
        
        
    }
    
    
    //按钮的点击事件
     func showPublish(){
        
        let publish = YHKPublishViewController()
        
        let nav = UINavigationController(rootViewController: publish)
        
        presentViewController(nav, animated: true) { () -> Void in
            
        }
        
        
    }
    
    
    
    
    
    
    
    
    
    //重载
    func addChildViewController() {
        
        addChildViewController(YHKHomeViewController(), title: "首页", image: "tabbar_home")

        addChildViewController(YHKMessageViewController(), title: "消息", image: "tabbar_message_center")
        
        addChildViewController(YHKFindViewController(), title: "发现", image: "tabbar_discover")
        
        addChildViewController(YHKSettingViewController(), title: "设置", image: "tabbar_profile")
    }
    
    //封装添加控制器
    func addChildViewController(childController: UIViewController,title:String,image:String) {
        
        
        childController.title = title
        
        childController.tabBarItem.image = UIImage(named: image)
        
        childController.tabBarItem.selectedImage = UIImage(named: "\(image)highlighted")
        //让系统不渲染我们的图片,已经在APPDelegate里设置了
//        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        addChildViewController(UINavigationController(rootViewController: childController))
        
        
    }
}
